package controller

import (
	"CampusCloudAid/logic"
	"CampusCloudAid/models"
	"net/http"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// PublishWorkOrders 创建工单接口
// @Summary 创建工单
// @Description 根据传入的参数创建工单
// @Tags 工单管理
// @Accept json
// @Produce json
// @Param id path int true "订单ID"
// @Param data body models.CreateWorkOrders true "data"
// @Router /api/v1/publishWorkOrders [post]
func PublishWorkOrders(c *gin.Context) {
	//获取参数，校验
	p := new(models.CreateWorkOrders)
	if err := c.ShouldBindJSON(p); err != nil {
		zap.L().Debug("c.ShouldBindJSON(&p) error", zap.Any("err", err))
		zap.L().Error("create task with in invalid param", zap.Error(err))
		ResponseError(c, CodeInvalidParams)
		return
	}
	//将当前订单id放入创建工单结构体中
	p.Id = p.TaskId
	p.Status = "未解决"
	//创建工单
	if err := logic.CreateWorkOrders(p); err != nil {
		zap.L().Error("logic.CreateWorkOrders failed", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}
	//返回响应
	ResponseSuccess(c, nil)
}

// 修改工单状态接口
// 修改工单状态接口
func ModifyWorkOrder(c *gin.Context) {
	// 解析JSON请求体
	var requestBody struct {
		OrderID   int64  `json:"orderID"`
		NewStatus string `json:"newStatus"`
	}
	if err := c.ShouldBindJSON(&requestBody); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON request body"})
		return
	}

	// 调用逻辑层方法修改工单
	err := logic.ModifyWorkOrder(requestBody.OrderID, requestBody.NewStatus)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	// 返回成功响应
	c.JSON(http.StatusOK, gin.H{"message": "Work order updated successfully"})
}
